<?php

namespace app\admin\model;

use think\Model;

class System extends Model
{

    //事件
    protected static function init()
    {
        self::beforeUpdate(function ($thisObj){
            $config = json_decode($thisObj->config,true);

            if (!empty($config['web_logo']) && stripos($config['web_logo'],'temp')){
                $config['web_logo'] = temp_to_uploads($config['web_logo']);
            }
            if (!empty($config['weixin_ma']) && stripos($config['weixin_ma'],'temp')){
                $config['weixin_ma'] = temp_to_uploads($config['weixin_ma']);
            }
            $thisObj->config = json_encode($config);
        });
        self::beforeInsert(function ($thisObj){
            $config = json_decode($thisObj->config,true);
            //博客头像
            if (!empty($config['web_logo'])){
                $config['web_logo'] = temp_to_uploads($config['web_logo']);
            }
            //微信二维码
            if (!empty($config['weixin_ma'])){
                $config['weixin_ma'] = temp_to_uploads($config['weixin_ma']);
            }
            $thisObj->config = json_encode($config);
        });
    }



    public static function mStore($data = [])
    {

        $res = self::get(['en_name' => $data['en_name']]);

        if (!$res) {
            $rel = self::create($data, TRUE);
        } else {
            $rel = self::update($data, ['en_name' => $data['en_name']], TRUE);
        }

        if (FALSE !== $rel) {
            return return_json(0, '更新成功');
        }

        return return_json(1, '更新失败');

    }
}
